//
// Created by hjie on 23-9-25.
//

#pragma once

#include <cstring>
#include <thread>
#include <atomic>
#include <mutex>
#include <vector>
#include <memory>
#include <functional>

using ThreadFunction = std::function<void()>;

class MsgQueue
{
public:
    explicit MsgQueue(int init_size);
    ~MsgQueue();
    void PushTask(ThreadFunction &&func);
    void AddReference();
    void UnReference();
public:
    int m_index{0};
    std::thread m_thread;
    std::mutex  m_mutex;
    std::vector<ThreadFunction> m_queue;
    std::atomic<int> m_reference_count;
};

class SignalThreadPool
{
public:
    SignalThreadPool();
public:
    void StartThread(int size);
    void StopThread();
    void OnThread(int index);
    std::shared_ptr<MsgQueue> GetMsgQueueByIndex(int index = 0);
private:
    std::vector<std::shared_ptr<MsgQueue>> m_msg;
    bool m_thread_pool_status{false};
};
